﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Bukstor.Biz;

namespace Bukstor.UI
{
    public partial class Formulario_web1 : System.Web.UI.Page
    {
        BukstorBDEntities Entities;

        protected void Page_Load(object sender, EventArgs e)
        {
            Entities = new BukstorBDEntities();
            IEnumerable<Categoria> categorias = from Categoria cat in Entities.Categorias select cat;
            foreach (Categoria cat in categorias)
            {
                TreeNode nodoCategoria = new TreeNode(cat.nombre, cat.id.ToString(), "", "CatSubcategorias.aspx?cat=" +
                    cat.id.ToString(), "_self");
                IEnumerable<Subcategoria> subcategorias = from Subcategoria sub in Entities.Subcategorias
                                                          where sub.Categoria.id == cat.id
                                                          select sub;
                subcategorias.Count();
                foreach (Subcategoria subcat in subcategorias)
                {
                    nodoCategoria.ChildNodes.Add(new TreeNode(subcat.nombre, subcat.nombre, null, "CatTitulos.aspx?subcat=" + 
                        subcat.id, "_self"));
                }
                treCategorias.Nodes.Add(nodoCategoria);
            }
        }

        protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e)
        {
            Response.Redirect("CatSubcategorias.aspx?cat=" + treCategorias.SelectedNode.Text, true);
        }
    }
}
